using System.Data.SQLite;
using Model;

namespace DAL {
    public class OrderDAL {
        private OrderDAL() {
        }

        private static OrderDAL _instance;

        public static OrderDAL GetInstance() {
            if (_instance == null) {
                _instance = new OrderDAL();
            }

            return _instance;
        }

        /// <summary>
        /// 插入订单返回订单id
        /// </summary>
        /// <param name="order"></param>
        /// <returns>返回插入的id</returns>
        public int InsertOrder(Order order) {
            return int.Parse(SqlHelper.GetSingleResult(
                new SQLiteCommand(
                    "insert into OrderInfo(NumberOfDiners,Remark,SubTime) values (@NumberOfDiners,@Remark,@SubTime);select last_insert_rowid();") {
                    Parameters = {
                        new SQLiteParameter("@NumberOfDiners", order.NumberOfDiners),
                        new SQLiteParameter("@Remark", order.Remark),
                        new SQLiteParameter("@SubTime", order.SubTime)
                    }
                }).ToString());
        }

        public void Update(Order order) {
            SqlHelper.Update(new SQLiteCommand("update OrderInfo set OrderState = @OrderState, EndTime = @endtime, OrderMoney = @OrderMoney, Discount = @discount where OrderId = @OrderId") {
                Parameters = {
                    new SQLiteParameter("@OrderId", order.OrderId),
                    new SQLiteParameter("@OrderState", order.OrderState),
                    new SQLiteParameter("@endtime", order.EndTime),
                    new SQLiteParameter("@discount", order.Discount),
                    new SQLiteParameter("@OrderMoney", order.OrderMoney)
                }
            });
        }
    }
}